package com.peas.platform.module.icims.base.auth.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.lang.Assert;
import com.peas.platform.framework.mybatisplus.domain.BaseDomain;
import com.peas.platform.framework.mybatisplus.po.BasePo;
import com.peas.platform.framework.mybatisplus.service.BaseServiceImpl;
import com.peas.platform.module.icims.base.auth.api.req.SaveUserReq;
import com.peas.platform.module.icims.base.auth.controller.vo.UserRoleVo;
import com.peas.platform.module.icims.base.auth.domain.UserDomain;
import com.peas.platform.module.icims.base.auth.dto.UserDto;
import com.peas.platform.module.icims.base.auth.dto.UserLoginDto;
import com.peas.platform.module.icims.base.auth.dto.UserRoleDto;
import com.peas.platform.module.icims.base.auth.enums.UserLoginState;
import com.peas.platform.module.icims.base.auth.service.UserLoginService;
import com.peas.platform.module.icims.base.auth.service.UserRoleService;
import com.peas.platform.module.icims.base.auth.service.UserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class UserServiceImpl extends BaseServiceImpl<UserDto> implements UserService {

    @Resource
    private UserDomain userDomain;
    @Resource
    private UserLoginService userLoginService;
    @Resource
    private UserRoleService userRoleService;

    @Override
    public BaseDomain<UserDto, ? extends BasePo> getDomain() {
        return userDomain;
    }

    @Transactional
    @Override
    public UserDto login(String username, String password, String ip) {
        Assert.notBlank(username, "账号不能为空");
        Assert.notBlank(password, "密码不能为空");
        UserDto c = new UserDto();
        c.setUsername(username);
        c.setPassword(password);
        UserDto user = this.getOne(c, false);
        Assert.notNull(user, "账号或密码错误");
        user.setLastLoginTime(DateTime.now());
        user.setLastLoginIp(ip);
        this.updateById(user);
        UserLoginDto uld = new UserLoginDto();
        uld.setUserId(user.getId());
        uld.setState(UserLoginState.success.getKey());
        uld.setLoginTime(DateTime.now());
        uld.setLoginIp(ip);
        userLoginService.save(uld);
        return user;
    }

    @Transactional
    @Override
    public void reset(Long userId, String password, String newPassword) {
        Assert.notNull(userId, "用户为空");
        Assert.notBlank(password, "密码不能为空");
        Assert.notBlank(newPassword, "新密码不能为空");
        UserDto user = this.getById(userId);
        Assert.equals(password, user.getPassword(), "密码输入错误");
        user.setPassword(newPassword);
        user.setNeedModify("0");
        this.updateById(user);
    }

    @Transactional
    @Override
    public void saveUser(SaveUserReq vo) {
        UserDto dto = BeanUtil.copyProperties(vo, UserDto.class);
        int result = dto.getId() == null ? this.save(dto) : this.updateById(dto);
        org.springframework.util.Assert.isTrue(result > 0, "保存失败");
        //roles
        List<UserRoleDto> urds = userRoleService.listByField("userId", dto.getId());
        userRoleService.removeBatchByList(urds);
        List<UserRoleDto> rpdList = new ArrayList<>();
        CollUtil.forEach(vo.getRoles(), (CollUtil.Consumer<UserRoleVo>) (v, index) -> {
            UserRoleDto rpd = new UserRoleDto();
            rpd.setUserId(dto.getId());
            rpd.setRoleId(v.getRoleId());
            rpdList.add(rpd);
        });
        userRoleService.saveBatch(rpdList);
    }

}
